#include "main.h"

//通讯相关
uint8 example_rx_buffer[OPENART_UART_LENGTH] = {0};
uint8 uart_data[OPENART_UART_LENGTH] = {0};
uint8 uart_last[OPENART_UART_LENGTH] = {0};
uint8 uart_cmd = 0;
uint8 uart_head = 0xb1;
uint8 uart_tail = 0xee;
lpuart_transfer_t   example_receivexfer;
lpuart_handle_t     example_g_lpuartHandle;

// 云台参数
uint16 pan_mid_pos = 27055;
uint16 pan_right_pos = 15455;
uint16 pan_left_pos = 38555;
uint16 pan_realtime_pos = 0;
uint16 delta_angle = 0;

// 时变信号
uint8 flag_reaction = 0;			//串口接收到数据后反应开关
uint8 flag_laser_shooting = 0;		//发射激光开关

//串口通讯回调函数（历程）
//--------------------UART参考-----------------------//
//1.uart_data:存放寄存器中的数据
//
//2.uart_last:存放最后一次发过来的数据,
//			[0]帧头：0xb0，
//			[1]CMD命令位，
//			[2]数据位1：存放云台旋转方向，
//			[3]数据位2：存放云台旋转角度，
//			[4]帧尾：0xee.
//---------------------------------------------------//
void uart_callback(LPUART_Type *base, lpuart_handle_t *handle, status_t status, void *userData)
{
    if(kStatus_LPUART_RxIdle == status)
    {
        //数据已经被写入到了 之前设置的BUFF中
        //本例程使用的BUFF为 example_rx_buffer
		if (example_rx_buffer[0] == 0xb0)
			if(example_rx_buffer[4] == 0xee)
			{
				for(int i=0;i<5;i++)
				{	
					uart_data[i] = example_rx_buffer[i];//将数据取出
					uart_last[i] = uart_data[i];
				}
			}
			else
			{
				for(int i=0;i<5;i++)
					{	
						uart_data[i] = 0x00;//将数据取出
					}
			}
		else
		{
			for(int i=0;i<5;i++)
				{	
					uart_data[i] = 0x00;//将数据取出
				}
		}	
    }
    flag_reaction = 1;
    handle->rxDataSize = example_receivexfer.dataSize;  //还原缓冲区长度
    handle->rxData = example_receivexfer.data;          //还原缓冲区地址
}


//发送串口数据，调用前需给uart_cmd赋值：uart_cmd = cmd_xxxx
void Uart_Send()
{
	uart_putchar(USART_2, uart_head);	//帧头
	uart_putchar(USART_2, uart_cmd);	//命令
	uart_putchar(USART_2, uart_tail);	//帧尾
}


void OPENARTCmdDetection()
{
	if(flag_reaction)
	{
		if(uart_last[1] == CMD_VERIFICATION_FAIL)
		{
			flag_laser_shooting = 0;
			delta_angle = uart_last[3];
			uint16 delta_duty = 315 * delta_angle;
			//判断旋转方向，即接收4字节中的数据字节（第2字节）正负
			if (uart_last[2]&0x80)
				pan_realtime_pos -= delta_duty;
			else 
				pan_realtime_pos += delta_duty;
			//Verification Sending,旋转舵机后，向OPENART发送坐标验证请求
			uart_cmd = CMD_AXIS_VERIFICATION;
			Uart_Send();
		}
		//如果OPENART发送来了坐标验证成功的指令，便发射激光
		else if(uart_last[1] == CMD_VERIFICATION_SUCCESS)
		{
			flag_laser_shooting = 1;
		}
		//如果OPENART发送来了停止发射激光的指令，停止发射激光
		else if(uart_last[1] == CMD_LASER_STOP)
		{
			flag_laser_shooting = 0;
			pan_realtime_pos = pan_mid_pos;
			pit_interrupt_ms(PIT_CH2, 100);
		}
		//如果OPENART发回了二维码结果：偶数，给OPENART发送识别命令，同时开始漫步
		if (uart_last[1] == CMD_APRILTAG_EVEN)
		{
			pan_realtime_pos = pan_left_pos;
			uart_cmd = CMD_FIND_ANIMAL_FRUIT;
			Uart_Send();
		}
		
		//如果OPENART发回了二维码结果：奇数，给OPENART发送识别命令，同时开始漫步
		if (uart_last[1] == CMD_APRILTAG_ODD)
		{
			pan_realtime_pos = pan_right_pos;
			uart_cmd = CMD_FIND_ANIMAL_FRUIT;
			Uart_Send();
		}
		//OPENART识别结果为动物
		else if (uart_last[1] == CMD_ANIMAL_DETECTED)
		{	
			//初始化云台朝向
			pan_realtime_pos = pan_mid_pos;
			// 订时三秒
			pit_interrupt_ms(PIT_CH2, 3000);
		}
		// 找到方框后，从漫步变为完全停止
		else if (uart_last[1] == CMD_STOP_CAR)
		{
			// 停车
			if(2 == run_switch[RUN_EMERGENCY_SWITCH])
				run_switch[RUN_EMERGENCY_SWITCH] = 3;
		}
		else if(uart_last[1] == CMD_Y_LEFT)
		{
			//run_switch[RUN_EMERGENCY_SWITCH] = 0; 
			run_switch[RUN_MODE_SWITCH] = IMAGE_MODE;
			// elements_flag[Y_JUNCTION] = 2;
			y_junction_direction[y_junction_count] = 1;
			pit_interrupt_ms(PIT_CH2, 100);
		}
		else if(uart_last[1] == CMD_Y_RIGHT)
		{
			//run_switch[RUN_EMERGENCY_SWITCH] = 0;
			run_switch[RUN_MODE_SWITCH] = IMAGE_MODE;
			//elements_flag[Y_JUNCTION] = 3;
			y_junction_direction[y_junction_count] = 2;
			pit_interrupt_ms(PIT_CH2, 100);
		}
		flag_reaction = 0;
	}
	//更新云台与激光的PWM
	pwm_duty( PWM_PAN, pan_realtime_pos);	//更新云台实时位置
	if (flag_laser_shooting)				//如果激光权限打开，则开始发射激光
		pwm_duty(PWM_LASER, 25000);
	else
		pwm_duty(PWM_LASER, 0);
}